package com.yimixian.app.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;
import com.yimixian.app.R;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.ImageInfo;
import com.ymx.sdk.util.DensityUtils;
import com.ymx.sdk.util.DeviceUtils;

import java.util.List;

/**
 * Created by zhilian-2 on 2015/10/15.
 */
public class SingleBannerLinearLayout extends LinearLayout{

    Picasso picasso;
    private DataManager mDataManager;
    int screenWidth;
    HomeHeadView.OnHeadViewClickListener mHeadViewClickListener;
    public SingleBannerLinearLayout(Context context) {
        super(context);
        initView();
    }

    public SingleBannerLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SingleBannerLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView(){
        setOrientation(VERTICAL);
        mDataManager = DataManager.getInstance();
        picasso = Picasso.with(getContext());
        screenWidth = DeviceUtils.getScreenWidth(getContext());
    }
    public void bind(List<ImageInfo> mList, float ratio, final HomeHeadView.OnHeadViewClickListener
            mHeadViewClickListener){
        if (mList == null) return;
        this.mHeadViewClickListener = mHeadViewClickListener;
        int height = (int)(screenWidth / ratio);
        for (int i = 0; i < mList.size(); i++){
            ImageInfo imageInfo = mList.get(i);
            ImageView imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, height);
            imageView.setTag(i);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mHeadViewClickListener != null) mHeadViewClickListener.OnHeadViewClick(
                            Constants.HeadViewType.SingleBanner, (int) view.getTag());
                }
            });
            imageView.setLayoutParams(params);
            params.topMargin = DensityUtils.dp2px(getContext(), 5);
            picasso.load(mDataManager.getImageUrl(imageInfo.image, screenWidth))
                    .placeholder(R.drawable.placeholder)
                    .into(imageView);
            this.addView(imageView);
        }
    }
}
